﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.Data;
using Maticsoft.Common;

namespace WEB.customer
{
    public partial class customerView : System.Web.UI.Page
    {

        public DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 页面载入

            gvCustomerManage.Attributes.Add("BorderColor", "black");
            gvCustomerManage.Attributes.Add("BorderWidth", "2");    //边框颜色
            if (!IsPostBack)
            { Bind(); }
            GroupRows(gvCustomerManage);
            #endregion
        }


        /// <summary>
        /// 搜索键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery1_Click(object sender, EventArgs e)
        {
            Bind();
        }

        /// <summary>
        /// 绑定方法
        /// </summary>
        protected void Bind()
        {
            string strQuery1 = ddlQuery1.SelectedValue;

            string strSql = "";
            if (tbQuery1.Text.Trim() != "")
            {
                switch (ddlQuery1.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        strSql = string.Format("{0} like '%{1}%'", strQuery1, tbQuery1.Text.Trim());
                        break;
                }
            }

            CustomerManage customerManage = new CustomerManage();
            DataSet ds = new DataSet();
            ds = customerManage.GetViewList(strSql);
            gvCustomerManage.DataSource = ds;
            gvCustomerManage.DataBind();



        }

        /// <summary>
        /// 重建表头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCustomerManage_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 建表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    //第一行
                    TableCellCollection th = e.Row.Cells;
                    th.Clear();
                    th.Add(new TableHeaderCell());

                    th[0].Attributes.Add("colspan", "9");
                    th[0].Text = "客户信息";
                    th.Add(new TableHeaderCell());
                    th[1].Attributes.Add("colspan", "10");
                    th[1].Text = "客户联系人信息 ";
                    th.Add(new TableHeaderCell());
                    th[2].Attributes.Add("colspan", "4");
                    th[2].Text = " </th></tr><tr>";

                    ////第二行
                    th.Add(new TableHeaderCell());
                    th[3].Text = "客户类型";
                    th.Add(new TableHeaderCell());
                    th[4].Text = "公司名";
                    th.Add(new TableHeaderCell());
                    th[5].Text = "税务识别号";
                    th.Add(new TableHeaderCell());
                    th[6].Text = "报检代码";
                    th.Add(new TableHeaderCell());
                    th[7].Text = "发票类型";
                    th.Add(new TableHeaderCell());
                    th[8].Text = "发票抬头";
                    th.Add(new TableHeaderCell());
                    th[9].Text = "邮编";
                    th.Add(new TableHeaderCell());
                    th[10].Text = "地址";
                    th.Add(new TableHeaderCell());
                    th[11].Text = "工作要求";
                    th.Add(new TableHeaderCell());
                    th[12].Text = "姓名";
                    th.Add(new TableHeaderCell());
                    th[13].Text = "性别";
                    th.Add(new TableHeaderCell());
                    th[14].Text = "职务";
                    th.Add(new TableHeaderCell());
                    th[15].Text = "手机";
                    th.Add(new TableHeaderCell());
                    th[16].Text = "固定电话";
                    th.Add(new TableHeaderCell());
                    th[17].Text = "传真";
                    th.Add(new TableHeaderCell());
                    th[18].Text = "Email";
                    th.Add(new TableHeaderCell());
                    th[19].Text = "备注";
                    th.Add(new TableHeaderCell());
                    th[20].Attributes.Add("colspan", "2");
                    th[20].Text = "联系人";
                    th.Add(new TableHeaderCell());
                    th[21].Attributes.Add("colspan", "3");
                    th[21].Text = "";
                    ////修改背景色
                    //th[3].Attributes.Add("bgcolor", "khaki");
                    //th[4].Attributes.Add("bgcolor", "khaki");
                    //th[5].Attributes.Add("bgcolor", "khaki");
                    //th[6].Attributes.Add("bgcolor", "khaki");
                    //th[7].Attributes.Add("bgcolor", "khaki");
                    //th[8].Attributes.Add("bgcolor", "khaki");
                    //th[9].Attributes.Add("bgcolor", "khaki");
                    //th[10].Attributes.Add("bgcolor", "khaki");
                    //th[11].Attributes.Add("bgcolor", "khaki");
                    //th[12].Attributes.Add("bgcolor", "khaki");
                    //th[13].Attributes.Add("bgcolor", "khaki");
                    //th[14].Attributes.Add("bgcolor", "khaki");
                    //th[15].Attributes.Add("bgcolor", "khaki");
                    //th[16].Attributes.Add("bgcolor", "khaki");
                    //th[17].Attributes.Add("bgcolor", "khaki");
                    //th[18].Attributes.Add("bgcolor", "khaki");
                    //th[19].Attributes.Add("bgcolor", "khaki");
                    //th[20].Attributes.Add("bgcolor", "khaki");
                    break;
            }
            #endregion
        }
        #region 行的合并
        /// <summary>
        /// 行的合并
        /// </summary>
        /// <param name="gv">gridview</param>
        /// <param name="col">合并列</param>
        public void GroupRows(GridView gv)
        {
            int i = 0, Rowspannums = 1;
            while (i < gv.Rows.Count - 1)
            {
                GridViewRow gvr = gv.Rows[i];
                int gvkey = (Int32)gv.DataKeys[i].Value;

                for (i++; i < gv.Rows.Count; i++)
                {
                    GridViewRow nextgvr = gv.Rows[i];
                    int ngvkey = (Int32)gv.DataKeys[i].Value;
                    if (gvkey == ngvkey)
                    {
                        nextgvr.Cells[0].Visible = false;
                        nextgvr.Cells[1].Visible = false;
                        nextgvr.Cells[2].Visible = false;
                        nextgvr.Cells[3].Visible = false;
                        nextgvr.Cells[4].Visible = false;
                        nextgvr.Cells[5].Visible = false;
                        nextgvr.Cells[6].Visible = false;
                        nextgvr.Cells[7].Visible = false;
                        nextgvr.Cells[8].Visible = false;
                        nextgvr.Cells[19].Visible = false;
                        nextgvr.Cells[20].Visible = false;
                        nextgvr.Cells[21].Visible = false;
                        Rowspannums++;
                    }
                    else
                    {
                        gvr.Cells[0].RowSpan = Rowspannums;//跨行数
                        gvr.Cells[1].RowSpan = Rowspannums;
                        gvr.Cells[2].RowSpan = Rowspannums;
                        gvr.Cells[3].RowSpan = Rowspannums;
                        gvr.Cells[4].RowSpan = Rowspannums;
                        gvr.Cells[5].RowSpan = Rowspannums;
                        gvr.Cells[6].RowSpan = Rowspannums;
                        gvr.Cells[7].RowSpan = Rowspannums;
                        gvr.Cells[8].RowSpan = Rowspannums;
                        gvr.Cells[19].RowSpan = Rowspannums;
                        gvr.Cells[20].RowSpan = Rowspannums;
                        gvr.Cells[21].RowSpan = Rowspannums;
                        Rowspannums = 1;
                        break;
                    }
                    if (i == gv.Rows.Count - 1)
                    {
                        gvr.Cells[0].RowSpan = Rowspannums;
                        gvr.Cells[1].RowSpan = Rowspannums;
                        gvr.Cells[2].RowSpan = Rowspannums;
                        gvr.Cells[3].RowSpan = Rowspannums;
                        gvr.Cells[4].RowSpan = Rowspannums;
                        gvr.Cells[5].RowSpan = Rowspannums;
                        gvr.Cells[6].RowSpan = Rowspannums;
                        gvr.Cells[7].RowSpan = Rowspannums;
                        gvr.Cells[8].RowSpan = Rowspannums;
                        gvr.Cells[19].RowSpan = Rowspannums;
                        gvr.Cells[20].RowSpan = Rowspannums;
                        gvr.Cells[21].RowSpan = Rowspannums;

                    }
                }
            }
        }
        #endregion

        #region 功能键
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (((Button)sender).CommandArgument.ToString() != "")
            {
                int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
                Response.Redirect("/contact/contactEdit.aspx?ID=" + id);
            }
            else
            {
                MessageBox.Show(this, "没有数据！");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            string id = ((Button)sender).CommandArgument.ToString();//获取id
            ContactManage con = new ContactManage();
            //判断
            if (id != "")
            {
                if (con.Delete(Convert.ToInt32(id)))
                {
                    MessageBox.ShowAndRedirect(this, "删除成功！", "/customer/customerView.aspx");

                }
                else
                {
                    MessageBox.Show(this, "删除失败！");

                }
                return;
            }
            else
            {
                MessageBox.Show(this, "没有数据！");
                return;
            }
        }
        protected void gvCustomerManage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();
            if (index != "")
            {
                try
                {
                    int id = e.CommandName == "" ? 0 : (Convert.ToInt32(gvCustomerManage.DataKeys[Convert.ToInt32(index)].Value));
                    if (e.CommandName == "Edit")//修改公司信息
                    {
                        //string id = Server.UrlEncode(ds.Tables[0].Rows[Convert.ToInt32(index)][0].ToString());
                        Response.Redirect("~/customer/customerEdit.aspx?ID=" + id);
                    }
                    else if (e.CommandName == "Add")
                    {
                        Response.Redirect("~/contact/contactEdit.aspx?AddID=" + id);
                    }
                    else if (e.CommandName == "conView")
                    {
                        Response.Redirect("~/contract/View.aspx?ID=" + id);
                    }
                    else if (e.CommandName == "addCon")
                    {
                        Response.Redirect("~/contract/Add.aspx?AddID=" + id);
                    }
                }
                catch
                { }

            }
        }
        #endregion

        protected void gvCustomerManage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            #region 鼠标事件
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='PeachPuff'}");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");
                //点击行变色 
                e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");
                //设置悬浮鼠标指针形状为"小手" 
                e.Row.Attributes["style"] = "Cursor:hand";

            }
            #endregion
        }


        /// <summary>
        /// 换页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCustomerManage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //换页功能
            this.gvCustomerManage.PageIndex = e.NewPageIndex;
            //绑定GridView
            gvCustomerManage.DataBind();
            TextBox tb = (TextBox)gvCustomerManage.BottomPagerRow.FindControl("tbinPageNum");
            tb.Text = (gvCustomerManage.PageIndex + 1).ToString();
            Bind();

        }

        /// <summary>
        /// 跳页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button_Click(object sender, EventArgs e)
        {
            try
            {
                gvCustomerManage.DataBind();
                TextBox tb = (TextBox)gvCustomerManage.BottomPagerRow.FindControl("tbinPageNum");
                int num = Int32.Parse(tb.Text.Trim());
                GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                gvCustomerManage_PageIndexChanging(null, ea);
            }
            catch { MessageBox.Show(this, "请输入数字页码"); }

        }

    }
}